<template>
  <div class="flexContent">
    <div class="add">
      <el-button class="addBtn" v-has="'goodsSellReportDetail_export'" @click="exportFn">导出</el-button>
      <el-button class="celBtn" @click="returnFn">返回</el-button>
    </div>
    <div class="table" id="tableBox" v-height>
      <div style="flex:1;">
        <vxe-table :column-config="{ resizable: true }" border class="tablebox" ref="xTable1"
          :row-config="{ isHover: true }" :scroll-y="{ enabled: true, gt: 21 }" style="width: 100%" height="650"
          size="medium" align="center">
          <vxe-column type="seq" width="80" title="序号" />
          <vxe-column field="storeName" title="门店名称" min-width="120" />
          <vxe-column field="commodityName" title="商品名称"
            :min-width="elTableAdaptationWidth('commodityName', tableData)" />
          <vxe-column field="barcode" title="商品条码" :min-width="elTableAdaptationWidth('barcode', tableData)" />
          <vxe-column field="type" title="商品类型" width="160" />
          <vxe-column field="salesNum" title="累计销量" min-width="150">
            <template #header="{ column }">
              <div style="display: flex;justify-content: center;">
                <span>累计销量</span>
                <span style="display: flex;flex-direction: column;">
                  <i style="cursor: pointer;" class="el-icon-caret-top iconCls"
                    :class="sort === '1' ? 'iconClsActived' : ''" @click="sortFn('1')"></i>
                  <i style="cursor: pointer;transform: translateY(-5px);" :class="sort === '0' ? 'iconClsActived' : ''"
                    class="el-icon-caret-bottom iconCls" @click="sortFn('0')"></i>
                </span>
              </div>
            </template>
          </vxe-column>
          <vxe-column field="salesVolume" title="累计销售额" min-width="150">
            <template #header="{ column }">
              <div style="display: flex;justify-content: center;">
                <span>累计销售额</span>
                <span style="display: flex;flex-direction: column;">
                  <i style="cursor: pointer;" class="el-icon-caret-top iconCls"
                    :class="sort === '3' ? 'iconClsActived' : ''" @click="sortFn('3')"></i>
                  <i style="cursor: pointer;transform: translateY(-5px);" :class="sort === '2' ? 'iconClsActived' : ''"
                    class="el-icon-caret-bottom iconCls" @click="sortFn('2')"></i>
                </span>
              </div>
            </template>
          </vxe-column>
        </vxe-table>
      </div>
      <div class="paginationBox">
        <el-pagination :current-page="page.pageNum" background :page-sizes="$pageSizes" :page-size="page.pageSize"
          layout="total,-> , sizes, prev, pager, next, jumper" :total="page.total" @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>
    </div>
  </div>
</template>

<script>
import FileSaver from 'file-saver';
import { reqSellStatisticDetail, reqExportSellDetail } from "@/api/report";
export default {
  name: "GoodsSellReportDetail",
  data() {
    return {
      id: '',
      type: '',
      name: '',
      barcode: '',
      sort: '0',
      tableData: [],
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      },
    };
  },
  created() {
    this.type = this.$route.query.type
    if(this.type == '0'){
      this.id = this.$route.query.id;
    }else if(this.type == '1'){
      this.barcode = this.$route.query.barcode;
    }else if(this.type == '2'){
      this.name = this.$route.query.name;
    }
    this.getSellStatisticDetail();
  },
  methods: {
    async getSellStatisticDetail() {      
      const res = await reqSellStatisticDetail({
        ...this.page,
        type: this.type,
        id: this.id,
        barcode: this.barcode,
        name: this.name,
        sort: this.sort
      });
      if (res.code === 200) {
        console.log(res);
        this.tableData = res.data.list;
        this.page.total = res.data.total;
        const $table = this.$refs.xTable1
        $table.loadData(this.tableData);
      } else {
        // this.$message.error(res.message);
      }
    },
    sortFn(sort) {
      this.sort = sort;
      this.getSellStatisticDetail();
    },
    // 获取条数改变
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.page.pageNum = 1;
      console.log(`每页 ${val} 条`);
      this.getSellStatisticDetail();
    },
    // 获取页数改变
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page.pageNum = val;
      this.getSellStatisticDetail();
    },
    returnFn() {
      this.closeTag();
    },
    exportFn(){
      this.$confirm('确认导出数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let response = await reqExportSellDetail({
          type: this.type,
          id: this.id,
          barcode: this.barcode,
          name: this.name,
          sort: this.sort 
        })
        let res = null;
        if (response.data.type === "application/json") {
          const reader = new FileReader();  //创建一个FileReader实例
          reader.readAsText(response.data, 'utf-8'); //读取文件,结果用字符串形式表示
          reader.onload = () => {//读取完成后,**获取reader.result**
            res = JSON.parse(reader.result)
            if (res.code !== 200) {
              this.$message.error(res.message);
              return;
            }
          }
        } else {
          const fileName = decodeURIComponent(response.headers["content-disposition"].split(";")[1].split("''")[1]);
          FileSaver.saveAs(response.data, fileName);
        }
      })
    }
  },
};
</script>

<style lang="scss" scoped>
.txtBox {
  padding-left: 20px;
  margin-bottom: 30px;

}

.btnBOX {
  text-align: center;
}
.iconCls:hover {
  color: #ee2b08 !important;
}

.iconClsActived {
  color: #ee2b08 !important;
}
</style>
